/* Copyright (c) 2025-2030 ShawnLi*/

import { daysList, monthsList } from '~/renderer/constants/dictionary';

export type QuickRange =
  | 'all'
  | 'today'
  | 'yesterday'
  | 'last-week'
  | 'last-month'
  | 'older';

export const compareDates = (first: Date, second: Date) => {
  return (
    first != null &&
    second != null &&
    first.getFullYear() === second.getFullYear() &&
    first.getMonth() === second.getMonth() &&
    first.getDate() === second.getDate()
  );
};

export const getSectionLabel = (date: Date) => {
  let prefix = '';
  const current = new Date();

  if (
    date.getFullYear() === current.getFullYear() &&
    date.getMonth() === current.getMonth()
  ) {
    if (current.getDate() === date.getDate()) {
      prefix = 'Today - ';
    } else if (current.getDate() - 1 === date.getDate()) {
      prefix = 'Yesterday - ';
    }
  }

  return `${prefix}${daysList[date.getDay()]}, ${monthsList[date.getMonth()]
    } ${date.getDate()}, ${date.getFullYear()}`;
};

export const formatTime = (date: Date) => {
  return `${date.getHours().toString().padStart(2, '0')}:${date
    .getMinutes()
    .toString()
    .padStart(2, '0')}`;
};

export const getNsTimestamp = () => {
  const milliseconds = Date.now();
  const nanosecondsSinceProcessStart = process.hrtime.bigint();
  const nanosecondsOffset = Number(nanosecondsSinceProcessStart % BigInt(1e9));
  return `${milliseconds}${nanosecondsOffset.toString().padStart(9, '0')}`;
}

export const nsTimestampToDate = (nsTimestamp: string) => {
  const millisecondsPart = nsTimestamp.slice(0, -9);
  const milliseconds = parseInt(millisecondsPart, 10);
  return new Date(milliseconds);
}


export const calRange = (selectedRange: QuickRange) => {
  if (selectedRange === 'all') return null;
  const current = new Date();
  const day = current.getDate();
  const month = current.getMonth();
  const year = current.getFullYear();

  let minDate: Date;
  let maxDate: Date;

  switch (selectedRange) {
    case 'today': {
      minDate = new Date(year, month, day, 0, 0, 0, 0);
      maxDate = new Date(year, month, day, 23, 59, 59, 999);
      break;
    }
    case 'yesterday': {
      minDate = new Date(year, month, day - 1, 0, 0, 0, 0);
      maxDate = new Date(year, month, day - 1, 23, 59, 59, 999);
      break;
    }
    case 'last-week': {
      let currentDay = current.getDay() - 1;
      if (currentDay === -1) currentDay = 6;
      minDate = new Date(year, month, day - currentDay - 7, 0, 0, 0, 0);
      maxDate = new Date(year, month, day - currentDay - 1, 0, 0, 0, 0);
      break;
    }
    case 'last-month': {
      minDate = new Date(year, month - 1, 1, 0, 0, 0, 0);
      maxDate = new Date(year, month, 0, 0, 0, 0, 0);
      break;
    }
    case 'older': {
      let currentDay = current.getDay() - 1;
      if (currentDay === -1) currentDay = 6;
      minDate = new Date(0);
      maxDate = new Date(year, month, day - currentDay - 7, 0, 0, 0, 0);
      break;
    }
  }

  return {
    min: minDate.getTime(),
    max: maxDate.getTime(),
  };
}